//
//  ItemFirstWuAndWanViewController.m
//  Lovelife
//
//  Created by apple on 15/4/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ItemFirstWuAndWanViewController.h"
#import "ItemFirstThressCanDetasilsModel.h"
#import "WebViewController.h"

#import "ItemFirstWuCell.h"
#import "UIImageView+WebCache.h"
#import "ItemFirstWuTitleView.h"
#import "ItemZeroModel.h"
#import "NightManager.h"

@interface ItemFirstWuAndWanViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,retain)UICollectionView *collection;

@property (nonatomic,retain)ItemFirstWuTitleView *headerTitle;//头部@“午餐”

@property (nonatomic,retain)UIActivityIndicatorView *acIntor;

@end

@implementation ItemFirstWuAndWanViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.headerTitle.frame = CGRectMake(0, 0, self.view.frame.size.width/2-50, 40);
    }
    return self;
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    
    flow.itemSize = CGSizeMake(self.view.frame.size.width/2-30, self.view.frame.size.height/6+80);
    flow.minimumInteritemSpacing = 10;
    
    flow.minimumLineSpacing = 10;
    
    flow.sectionInset = UIEdgeInsetsMake(15, 5, 25, 20);
    
    flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width/2-50, 40);
    
    self.collection = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flow];
    
    self.collection.backgroundColor = [UIColor colorWithRed:247/255.0 green:248/255.0 blue:236/255.0 alpha:1];
    
    self.collection.dataSource = self;
    
    self.collection.delegate = self;
    
    [self.view addSubview:self.collection];
    
    [_collection release];
    [self.collection registerClass:[ItemFirstWuCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
    
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ItemFirstWuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    ItemFirstThressCanDetasilsModel *wu = self.wuModelArr[indexPath.row];
    
    [cell.Img sd_setImageWithURL:[NSURL URLWithString:wu.thumb]];
    
    cell.title.text = wu.title;
    
    return cell;
    
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

        
        self.headerTitle.LB.font = [UIFont systemFontOfSize:20];
        
        [head addSubview:self.headerTitle];
        
        return head;
    }else{
        
        return 0;
    }
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ItemZeroModel *zao = self.wuModelArr[0];
        
        WebViewController *controller = [[WebViewController alloc]init];
        
        zao.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",zao.ID];
        
        controller.model = zao;
        
        controller.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",zao.ID];
        
        [self.navigationController pushViewController:controller animated:YES];
    }else if (indexPath.row == 1)
    {
        
        ItemZeroModel *wu = self.wuModelArr[1];
        
        WebViewController *controller = [[WebViewController alloc]init];
        
        wu.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",wu.ID];
        
        controller.model = wu;
        
        controller.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",wu.ID];
        
        [self.navigationController pushViewController:controller animated:YES];
        
    }else
    {
        
        ItemZeroModel *wan = self.wuModelArr[2];
        
        WebViewController *controller = [[WebViewController alloc]init];
        
        wan.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",wan.ID];
        
        controller.model= wan;
        
        controller.url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@",wan.ID];
        
        [self.navigationController pushViewController:controller animated:YES];
        
    }
    
    
}
-(void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
    [NightManager setBackgroundColorWithCollectionView:self.collection];
    
}

-(void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
    [NightManager setBackgroundColorWithCollectionView:self.collection];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
